package chaincode

import "fmt"

const (
	ERR_LENGTH_TYPE              uint16 = 1000
	ERR_UNKNOWN_TYPE             uint16 = 1001
	ERR_STRING_TYPE              uint16 = 1002
	ERR_INT64_TYPE               uint16 = 1003
	ERR_FLOAT64_TYPE             uint16 = 1004
	ERR_ARRAY_TYPE               uint16 = 1005
	ERR_BOOL_TYPE                uint16 = 1006
	ERR_GET_TRADER               uint16 = 1007
	ERR_UNMARSHAL                uint16 = 1008
	ERR_MARSHAL                  uint16 = 1009
	ERR_GET_STATE                uint16 = 1010
	ERR_PUT_STATE                uint16 = 1011
	ERR_INVALID_FUNC             uint16 = 1012
	ERR_NUM_PARAME               uint16 = 1013
	ERR_EMPTY_PARAME             uint16 = 1014
	ERR_CREATE_COMPKEY           uint16 = 1015
	ERR_PARTICAL_COMPKEY         uint16 = 1016
	ERR_ITERATOR_NEXT            uint16 = 1017
	ERR_SPLIT_COMPKEY            uint16 = 1018
	ERR_FIELDNAME_MAXLEN         uint16 = 1019
	ERR_TRANSFER_STR_TYPE        uint16 = 1020
	ERR_TRANSFER_INT64_TYPE      uint16 = 1021
	ERR_TRANSFER_FLOAT64_TYPE    uint16 = 1022
	ERR_TRANSFER_BOOL_TYPE       uint16 = 1023
	ERR_DECODE_MODMETA           uint16 = 1024
	ERR_UID_MISSING              uint16 = 1025
	ERR_UNAME_MISSING            uint16 = 1026
	ERR_NOT_EXIST                uint16 = 1027
	ERR_GETQUERY_PAGINATION      uint16 = 1028
	ERR_GET_HISTORYKEY           uint16 = 1029
	ERR_HISTORYKEY_ITERATOR_NEXT uint16 = 1030
	ERR_MSPMAP_NOT_INIT          uint16 = 1031
	ERR_MSPID_NOT_REGISTER       uint16 = 1032
	ERR_CARDID_NOT_NULL          uint16 = 1033
	ERR_INDEX_NO_FIND_USER       uint16 = 1034
	ERR_NOT_AUTH_TO_UPDATE       uint16 = 1035
	ERR_DEL_STATE                uint16 = 1036
	ERR_FIELDKV_NOT_NULL         uint16 = 1037
	ERR_KV_EXIST                 uint16 = 1038
	ERR_SET_EVENT                uint16 = 1039
	ERR_CHAINCODE_PAINC          uint16 = 1040
	ERR_NAME_IS_NULL             uint16 = 1041
	ERR_OLDCARD_IS_NULL          uint16 = 1042
)

var ErrMsgEN = map[uint16]string{
	ERR_LENGTH_TYPE:              "input type is not long enough",
	ERR_UNKNOWN_TYPE:             "unknown type",
	ERR_STRING_TYPE:              "string type format error",
	ERR_INT64_TYPE:               "int64 type format error",
	ERR_FLOAT64_TYPE:             "float64 type format error",
	ERR_ARRAY_TYPE:               "array type format error",
	ERR_BOOL_TYPE:                "bool type format error",
	ERR_GET_TRADER:               "failed to get trader",
	ERR_UNMARSHAL:                "failed to unmarshal",
	ERR_MARSHAL:                  "failed to marshal",
	ERR_GET_STATE:                "failed to get chaincode state",
	ERR_PUT_STATE:                "failed to put chaincode state",
	ERR_INVALID_FUNC:             "invalid contract function name",
	ERR_NUM_PARAME:               "number of input parameters is incorrect",
	ERR_EMPTY_PARAME:             "input parameter cannot be empty",
	ERR_CREATE_COMPKEY:           "failed to create composite key",
	ERR_PARTICAL_COMPKEY:         "failed get state partial composite key",
	ERR_ITERATOR_NEXT:            "failed to iterator result next",
	ERR_SPLIT_COMPKEY:            "failed to split composite key",
	ERR_FIELDNAME_MAXLEN:         "field name greater than 128 byte",
	ERR_TRANSFER_STR_TYPE:        "failed to transfer string type",
	ERR_TRANSFER_INT64_TYPE:      "failed to transfer int64 type",
	ERR_TRANSFER_FLOAT64_TYPE:    "failed to transfer float64 type",
	ERR_TRANSFER_BOOL_TYPE:       "failed to transfer bool type",
	ERR_DECODE_MODMETA:           "failed to decode model meta",
	ERR_UID_MISSING:              "failed to uid field is missing",
	ERR_UNAME_MISSING:            "failed to uid field is missing",
	ERR_NOT_EXIST:                "assets data not exist",
	ERR_GETQUERY_PAGINATION:      "failed to get query result with pagination",
	ERR_GET_HISTORYKEY:           "failed to get history for key",
	ERR_HISTORYKEY_ITERATOR_NEXT: "failed to historyforkey iterator next",
	ERR_MSPMAP_NOT_INIT:          "failed to msp id not init",
	ERR_MSPID_NOT_REGISTER:       "failed to mspid not exist register or call get_orginfo function for the first time",
	ERR_CARDID_NOT_NULL:          "failed to cardid not null",
	ERR_INDEX_NO_FIND_USER:       "user does not exist or the index cannot be found",
	ERR_NOT_AUTH_TO_UPDATE:       "failed to not auth to update this record",
	ERR_DEL_STATE:                "failed to delete chaincode state",
	ERR_FIELDKV_NOT_NULL:         "failed to update kv field cannot be null",
	ERR_KV_EXIST:                 "the data already exists",
	ERR_SET_EVENT:                "failed to set event",
	ERR_CHAINCODE_PAINC:          "chaincode painc",
	ERR_NAME_IS_NULL:             "name is null",
	ERR_OLDCARD_IS_NULL:          "oldcard is null",
}

var ErrMsgCN = map[uint16]string{
	ERR_LENGTH_TYPE:              "输入参数非法",
	ERR_UNKNOWN_TYPE:             "未知数据类型",
	ERR_STRING_TYPE:              "字符串格式化错误",
	ERR_INT64_TYPE:               "整形格式化错误",
	ERR_FLOAT64_TYPE:             "浮点型格式化错误",
	ERR_ARRAY_TYPE:               "数组格式化错误",
	ERR_BOOL_TYPE:                "布尔类型格式化错误",
	ERR_GET_TRADER:               "获取操作者失败",
	ERR_UNMARSHAL:                "反序列化失败",
	ERR_MARSHAL:                  "序列化失败",
	ERR_GET_STATE:                "获取合约state失败",
	ERR_PUT_STATE:                "写合约state失败",
	ERR_INVALID_FUNC:             "合约函数不存在",
	ERR_NUM_PARAME:               "参数个数不正确",
	ERR_EMPTY_PARAME:             "参数不能为空",
	ERR_CREATE_COMPKEY:           "构建复合键失败",
	ERR_PARTICAL_COMPKEY:         "分离复合键失败",
	ERR_ITERATOR_NEXT:            "遍历迭代器失败",
	ERR_SPLIT_COMPKEY:            "切分复合键失败",
	ERR_FIELDNAME_MAXLEN:         "输入名称不能大于128字节",
	ERR_TRANSFER_STR_TYPE:        "转换字符串类型失败",
	ERR_TRANSFER_INT64_TYPE:      "转换整形失败",
	ERR_TRANSFER_FLOAT64_TYPE:    "转换浮点型失败",
	ERR_TRANSFER_BOOL_TYPE:       "转化布尔型失败",
	ERR_DECODE_MODMETA:           "解码元数据模板失败",
	ERR_UID_MISSING:              "用户ID不能为空",
	ERR_UNAME_MISSING:            "用户ID缺失",
	ERR_NOT_EXIST:                "数据不存在",
	ERR_GETQUERY_PAGINATION:      "分页查询失败",
	ERR_GET_HISTORYKEY:           "获取历史变更key失败",
	ERR_HISTORYKEY_ITERATOR_NEXT: "迭代历史变更器失败",
	ERR_MSPMAP_NOT_INIT:          "机构未初始化",
	ERR_MSPID_NOT_REGISTER:       "机构未注册或合约启动应首次调用get_orginfo接口填充缓存",
	ERR_CARDID_NOT_NULL:          "卡ID不能为空",
	ERR_INDEX_NO_FIND_USER:       "用户不存在或检索索引不存在",
	ERR_NOT_AUTH_TO_UPDATE:       "无权更新",
	ERR_DEL_STATE:                "删除合约state失败",
	ERR_FIELDKV_NOT_NULL:         "更新KV字段不能为空",
	ERR_KV_EXIST:                 "数据已存在",
	ERR_SET_EVENT:                "设置合约事件失败",
	ERR_CHAINCODE_PAINC:          "合约崩溃",
	ERR_NAME_IS_NULL:             "姓名不能为空",
	ERR_OLDCARD_IS_NULL:          "老卡不存在",
}

type ErrInfo struct {
	ErrCode uint16 `json:"code"`
	ErrMsg  string `json:"msg"`
	GoErr   error  `json:"err"`
}

func NewErrInfo(code uint16, err error) *ErrInfo {
	return &ErrInfo{
		ErrCode: code,
		ErrMsg:  ErrMsgCN[code],
		GoErr:   err,
	}
}

func (e *ErrInfo) Error() string {
	if e.GoErr != nil {
		return fmt.Sprintf(`{"code":%d,"msg":"%s","err":"%s"}`, e.ErrCode, e.ErrMsg, e.GoErr.Error())
	}
	return fmt.Sprintf(`{"code":%d,"msg":"%s"}`, e.ErrCode, e.ErrMsg)
}
